<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quanlysanpham extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 Thu
	 */
	function __construct()
    {
        parent::__construct();	
		$this->data['tieude']="Quản lý sản phẩm";
		$this->loadadmin();
	}
	public function index()
	{	
		$this->load->model('Sanpham_model');	
		$this->data=$this->Sanpham_model->getresult_index();
		
		
		$this->load->model('Danhmuc_model');
		$this->data['danhmucs']=$this->Danhmuc_model->getresult();
		
		$this->load->view('sanpham/sanpham',$this->data);
		$this->load->view('layout/footer');
		
	}
	public function xoa($masp)
	{
		$this->load->model('Sanpham_model');
		$this->Sanpham_model->delete($masp);
		$dt= $this->input->get();
		if(empty($dt['page']))
		{
			$dt['page']=1;
		}
		if(!empty($dt['key'])&&!empty($dt['dm']))	
		redirect("/Quanlysanpham/index/?key={$dt['key']}&dm={$dt['dm']}&page={$dt['page']}", 'refresh');
		else if(!empty($dt['key']))
		redirect("/Quanlysanpham/index/?key={$dt['key']}&page={$dt['page']}", 'refresh');
		else if(!empty($dt['dm']))
		redirect("/Quanlysanpham/index/?dm={$dt['dm']}&page={$dt['page']}", 'refresh');
		else 
    	redirect("/Quanlysanpham/index/?page={$dt['page']}", 'refresh');
	}
	public function chitiet($masp)
	{
		$this->load->model('Sanpham_model');
		$this->data['sanpham']=$this->Sanpham_model->getsanpham($masp);
		$this->data['hinhs']=$this->Sanpham_model->getresult_hinh($masp);
		$this->load->view('sanpham/chitietsanpham',$this->data);
		$this->load->view('layout/footer');
	}
	
	public function them()
	{
		$this->load->model('Danhmuc_model');
		$this->data['danhmucs']=$this->Danhmuc_model->getresult();

		$this->load->view('sanpham/themsanpham',$this->data);
		$this->load->view('layout/footer');
	}
	public function submit_them()
	{
		$this->load->library('form_validation');
		$this->load->model('Danhmuc_model');
		$this->data['danhmucs']=$this->Danhmuc_model->getresult();
		$filHinhDaiDien = "filHinhDaiDien";
		$filHinh = "filHinh";
		
		
		if($this->form_validation->run('themsanpham')==false ){
			$this->load->view('sanpham/themsanpham', $this->data);
		}
		elseif(!isset($_FILES[$filHinhDaiDien])||($_FILES[$filHinhDaiDien]['error']==4))
		{	
				$this->data['thongtinsanpham']=	$this->input->post();	
				$this->data['danhmuc']=$this->data['thongtinsanpham']['ma_dm'];
				$this->data['img']['error_upload']='Chưa chọn hình đại diện';
				$this->load->view('sanpham/themsanpham', $this->data);
		}
		else {	
			
			$this->load->helper('date');
			$this->data['img']=	$this->upload_process($filHinhDaiDien);	
			$this->data['lstimg']=$this->upload_process_list($filHinh);
			
			$this->data['thongtinsanpham']=	$this->input->post();	
			if(!empty($this->data['img']['image_full_url']))
			{
	
				$now=getdate();
				$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
				
				$sanpham=array(
					//'masp'=>'2',
					'tensanpham'=>$this->data['thongtinsanpham']['tensanpham'],
					'ma_dm'=>$this->data['thongtinsanpham']['ma_dm'],
					'mota'=>$this->data['thongtinsanpham']['mota'],
					'dongia'=>$this->data['thongtinsanpham']['dongia'],
					'soluong'=>$this->data['thongtinsanpham']['soluong'],
					'chitiet'=>$this->data['thongtinsanpham']['chitiet'],
					'hinh'=>$this->data['img']['image_full_url'],
					'donvitinh'=>$this->data['thongtinsanpham']['donvitinh'],
					'ngaynhap'=>$currentDate,
					'soluotxem'=>'0',
					'soluotdathang'=>'0',
					'mota_en'=>$this->data['thongtinsanpham']['mota_en'],
					'chitiet_en'=>$this->data['thongtinsanpham']['chitiet_en']
					);
				
				$this->data['sanpham']=$sanpham;		
				$this->load->model('Sanpham_model');
				$id=$this->Sanpham_model->insert($this->data['sanpham']);
				foreach ($this->data['lstimg'] as $item)
				{
					if(!empty($item['image_full_url']))
					{
						$hinh=array(
							'ma_sp'=>$id,
							'tenhinh'=>$item['image_full_url'],
						);	
						$this->Sanpham_model->insert_hinh($hinh);
					}
				}	
				$this->data['thongbao']="Thêm thành công sản phẩm ".$this->data['thongtinsanpham']['tensanpham']."!";	
			}
			$this->data['danhmuc']=$this->data['thongtinsanpham']['ma_dm'];
			$this->load->view('sanpham/themsanpham',$this->data);
		}	
		$this->load->view('layout/footer');
	}
	
	public function sua($masp)
	{		
		$this->load->library('form_validation');
		$this->load->model('Danhmuc_model');
		$this->load->model('Sanpham_model');
		
		$this->data['danhmucs']=$this->Danhmuc_model->getresult();
				
		$this->data['thongtinsanpham']=$this->input->post();
		
		if(!empty($this->data['thongtinsanpham']))
		{
			if($this->form_validation->run('themsanpham') ){
				$filHinhDaiDien = "filHinhDaiDien";
				$filHinh = "filHinh";	
				
				if(!($_FILES[$filHinhDaiDien]['error']==4))
				{	
					$this->data['img']=	$this->upload_process($filHinhDaiDien);	
					$this->data['lstimg']=$this->upload_process_list($filHinh);
					if(!empty($this->data['img']['image_full_url']))
					{
						$this->data['thongtinsanpham']=$this->input->post();	
						$this->data['thongtinsanpham']['hinh']=$this->data['img']['image_full_url'];
						$this->Sanpham_model->xoahinhsanpham($masp);		
						$this->Sanpham_model->update($this->data['thongtinsanpham'],$masp);
						foreach ($this->data['lstimg'] as $item)
						{
							if(!empty($item['image_full_url']))
							{
								$hinh=array(
									'ma_sp'=>$masp,
									'tenhinh'=>$item['image_full_url'],
								);	
								$this->Sanpham_model->insert_hinh($hinh);
							}
						}	
					}
					
				}else
				{
					
						$this->data['lstimg']=$this->upload_process_list($filHinh);
						$this->data['thongtinsanpham']=$this->input->post();
						$this->Sanpham_model->update($this->data['thongtinsanpham'],$masp);
						foreach ($this->data['lstimg'] as $item)
						{
							if(!empty($item['image_full_url']))
							{
								$hinh=array(
									'ma_sp'=>$masp,
									'tenhinh'=>$item['image_full_url'],
								);	
								$this->Sanpham_model->insert_hinh($hinh);
							}
						}	
				}		
			}	
		}
		$this->data['sanpham']=$this->Sanpham_model->getsanpham($masp);
		$this->data['hinhs']=$this->Sanpham_model->getresult_hinh($masp);
		$this->load->view('sanpham/suasanpham',$this->data);
		$this->load->view('layout/footer');
	}
	public function upload_process($upload_field) {
		$result = array();
		
		// Upload file
		$this->load->config('upload.php');
		$upload_config = $this->config->item('upload');
		
		$this->load->library('upload', $upload_config);
		$this->upload->initialize($upload_config);
		
		if (!$this->upload->do_upload($upload_field))
		{
			// Upload bị lỗi
			$result['error_upload'] = $this->upload->display_errors();
			$result['image_full_url'] = '';
		}
		else
		{
			// Upload file successfull
			$result['upload_data'] = $this->upload->data();
			
			if (isset($result['upload_data']['is_image']) && $result['upload_data']['is_image'] == true) {
				// File vừa tải lên là file ảnh hợp lệ
				$result['image_full_url'] =  $upload_config['upload_path'] . $result['upload_data']['file_name'];
			}
			else {
				// File vừa tải lên không phải là file ảnh hợp lệ
				$result['image_full_url'] = '';
			}
		}
		
		return $result;
	}
	public function upload_process_list($upload_field) {
	
		$result=array();
		// Upload file
		$this->load->config('upload.php');
		$upload_config = $this->config->item('upload');
		$this->load->library('upload', $upload_config);
		$db=$_FILES[$upload_field];

		for ($i = 0; $i < count($db['name']); $i++)
		{
			$_FILES[$upload_field]['name']     = $db['name'][$i];
			$_FILES[$upload_field]['type']     = $db['type'][$i];
			$_FILES[$upload_field]['tmp_name'] = $db['tmp_name'][$i];
			$_FILES[$upload_field]['error']    = $db['error'][$i];
			$_FILES[$upload_field]['size']     = $db['size'][$i];
			$bd1=$_FILES[$upload_field];
			$this->upload->initialize($upload_config);
				$resultsub = array();
				if (!$this->upload->do_upload($upload_field))
				{
					// Upload bị lỗi
					$resultsub['error_upload'] = $this->upload->display_errors();
					$resultsub['image_full_url'] = '';
				}
				else
				{
					// Upload file successfull
					$resultsub['upload_data'] = $this->upload->data();
					if (isset($resultsub['upload_data']['is_image']) && $resultsub['upload_data']['is_image'] == true) {
						// File vừa tải lên là file ảnh hợp lệ
						$resultsub['image_full_url'] =  $upload_config['upload_path'] . $resultsub['upload_data']['file_name'];
					}
					else {
						// File vừa tải lên không phải là file ảnh hợp lệ
						$resultsub['image_full_url'] = '';
					}
				}
				$result[$i]=$resultsub;
		}
		//print_r($result);
		return $result;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */